package com.alkaid.anime.service.impl;

import com.alkaid.anime.mapper.AnimeMapper;
import com.alkaid.anime.mapper.AnimeVideoMapper;
import com.alkaid.anime.service.AnimeService;
import com.alkaid.commons.entity.Anime;
import com.alkaid.commons.entity.AnimePageEntity;
import com.alkaid.commons.entity.AnimeVideo;
import com.alkaid.commons.ex.AnimeNotFoundException;
import com.alkaid.commons.ex.DataNotFoundException;
import com.alkaid.commons.ex.IllegalException;
import com.alkaid.commons.ex.VideoNotFoundException;
import com.alkaid.commons.param.anime.AnimePlay;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Kylin
 */
@Service
public class AnimeServiceImpl implements AnimeService {

    private final AnimeMapper animeMapper;
    private final AnimeVideoMapper animeVideoMapper;

    public AnimeServiceImpl(AnimeMapper animeMapper, AnimeVideoMapper animeVideoMapper) {
        this.animeMapper = animeMapper;
        this.animeVideoMapper = animeVideoMapper;
    }

    @Override
    public List<Anime> getHotList(Integer num) throws IllegalException {
        LambdaQueryWrapper<Anime> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Anime::getId, Anime::getName, Anime::getHot).orderByDesc(Anime::getHot)
                .last("limit " + num);
        List<Anime> list = animeMapper.selectList(wrapper);
        if (list == null || list.size() == 0) {
            throw new DataNotFoundException();
        }
        return list;
    }

    @Override
    public List<Anime> getCountList(Integer num) throws IllegalException {
        LambdaQueryWrapper<Anime> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Anime::getId, Anime::getName, Anime::getCover, Anime::getCount, Anime::getTag, Anime::getAllNum,
                Anime::getRefreshNum, Anime::getStatus).orderByDesc(Anime::getCount).last("limit " + num);
        List<Anime> list = animeMapper.selectList(wrapper);
        if (list == null || list.size() == 0) {
            throw new DataNotFoundException();
        }
        return list;
    }

    @Override
    public PageInfo<Anime> getAll(AnimePageEntity page) throws VideoNotFoundException {
        PageHelper.startPage(page.getPage(), page.getRows(), page.getOrderBy());
        Page<Anime> list = animeMapper.findAll(page.getSql());
        return new PageInfo<>(list);
    }

    @Override
    public AnimePlay getAnimePlay(Integer id) throws AnimeNotFoundException, VideoNotFoundException {
        if (id == null) {
            return null;
        }
        Anime anime = animeMapper.selectById(id);
        if (anime == null) {
            throw new AnimeNotFoundException("番剧未找到");
        }
        List<AnimeVideo> animeVideos = animeVideoMapper.findByAnimeId(id);
        if (animeVideos.size() == 0) {
            throw new VideoNotFoundException("视频未找到");
        }
        return new AnimePlay(anime, animeVideos);
    }
}
